#include <iostream>
#include <cstdlib>
using namespace std;
//使用懒汉模式，写出线程安全的单例,先写成线程不安全的形式
class singleton{
public:
  static  singleton* getInstance(){
        if(_ps==nullptr){
            atexit(destroy);
            _ps=new singleton();
            cout<<"getInstance"<<endl;
        }cout<<"1"<<endl;
        return _ps;
    }
private:
 static void destroy(){
      if(_ps){
          cout<<"destroy"<<endl;
          delete _ps;
          _ps=nullptr;
      }
  }
    static singleton* _ps;
    singleton(){
        cout<<"singleton"<<endl;
    }
    ~singleton(){
        cout<<"~singleton"<<endl;
    }
};
    singleton* singleton::_ps=nullptr;
void test(){
    singleton::getInstance();
    singleton::getInstance();
    for(int i=0;i<10;i++){
        singleton::getInstance();
    }
}
int main(int argc, char * argv[]){
    test();
    return 0;
}

